using System;
using System.Drawing;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// The <see cref="IBOSelectorAndEditor"/> for editing the <see cref="DMUniqueConstraint"/>s of the <see cref="DMClass"/>.
    /// </summary>
    public class DMUniqueConstraintColEditorControl : PanelWin, IFormControl, IBOParentChildrenSelector
    {
        private DMClass _dmClass;

        /// <summary>
        /// Gets and Sets the selector control used to select a Business Object UniqueConstraint.
        /// </summary>
        public IBOParentChildrenSelector UniqueConstraintSelectorControl { get; private set; }

        /// <summary>
        /// Gets and sets the Control used to Edit <see cref="DMUniqueConstraint"/>s
        /// </summary>
        public IBusinessObjectControl UniqueConstraintEditorControl { get; private set; }

        public DMUniqueConstraintColEditorControl(IControlFactory controlFactory)
        {
            UniqueConstraintSelectorControl = new DMUniqueConstraintSelectorControl(controlFactory) {MinimumSize = new Size(355, 120)};

            UniqueConstraintEditorControl = new DMUniqueConstraintEditorControl(controlFactory);
            //UniqueConstraintEditorControl.MinimumSize = new Size(400, 120);
            this.Width = 755;
            this.Height = UniqueConstraintEditorControl.MinimumSize.Height;
            this.MinimumSize = new Size(this.Width, this.Height);
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(UniqueConstraintSelectorControl, BorderLayoutManager.Position.West);
            layoutManager.AddControl(UniqueConstraintEditorControl, BorderLayoutManager.Position.Centre, true);
            new BOSelectorAndEditorManager(UniqueConstraintSelectorControl, UniqueConstraintEditorControl);
            GridControl.BusinessObjectSelected += (sender, e) => FireBusinessObjectSelected(e);
            this.Enabled = false;
        }

        private void FireBusinessObjectSelected(BOEventArgs e)
        {
            if (this.BusinessObjectSelected != null)
            {
                this.BusinessObjectSelected(this, new BOEventArgs(e.BusinessObject));
            }
        }


        /// <summary>
        /// The <see cref="DMClass"/> whose <see cref="DMUniqueConstraint"/>s are being edited by this control
        /// </summary>
        public DMClass Class
        {
            get { return _dmClass; }
            set
            {
                _dmClass = value;
                UniqueConstraintSelectorControl.BusinessObject = _dmClass;
                UpdateState();
            }
        }

        private void UpdateState()
        {
            this.Enabled = _dmClass != null;
        }


        /// <summary>
        /// The <see cref="IBusinessObjectCollection"/> of <see cref="DMUniqueConstraint"/>s  being managed by this control.
        /// </summary>
        public BusinessObjectCollection<DMUniqueConstraint> UniqueConstraints
        {
            get
            {
                return 
                    (BusinessObjectCollection<DMUniqueConstraint>) BusinessObjectCollection;
            }
        }

        /// <summary>
        /// The <see cref="DMUniqueConstraint"/> currently selected in the Grid (i.e. currently being edited)
        /// </summary>
        public DMUniqueConstraint UniqueConstraint
        {
            get { return (DMUniqueConstraint) UniqueConstraintSelectorControl.SelectedBusinessObject; }
            set { UniqueConstraintSelectorControl.SelectedBusinessObject = value; }
        }


        public void SetForm(IFormHabanero form)
        {
        }

        public IBusinessObject BusinessObject
        {
            get { return this.Class; }
            set { this.Class = (DMClass) value;}
        }

        public event EventHandler<BOEventArgs> BusinessObjectSelected;

        public void Clear()
        {
            GridControl.Clear();
        }

        public IBusinessObject GetBusinessObjectAtRow(int row)
        {
            return GridControl.GetBusinessObjectAtRow(row);
        }

        public IBusinessObjectCollection BusinessObjectCollection
        {
            get { return GridControl.BusinessObjectCollection; }
            set { GridControl.BusinessObjectCollection = value; }
        }

        public IBusinessObject SelectedBusinessObject
        {
            get { return GridControl.SelectedBusinessObject; }
            set { GridControl.SelectedBusinessObject = value; }
        }

        public int NoOfItems
        {
            get { return GridControl.NoOfItems; }
        }

        /// <summary>
        ///             Gets and sets whether this selector autoselects the first item or not when a new collection is set.
        /// </summary>
        public bool AutoSelectFirstItem { get; set; }

        /// <summary>
        /// returns the underlying grid control used to display and select a collection 
        /// of DMProperties
        /// </summary>
        public IGridControl GridControl
        {
            get { return  ((DMUniqueConstraintSelectorControl)UniqueConstraintSelectorControl).GridControl ; }
        }
    }
}